// {    "openid":" OPENID",
//     " nickname": NICKNAME,
//     "sex":"1",
//     "province":"PROVINCE"
//     "city":"CITY",
//     "country":"COUNTRY",
//     "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ
//     4eMsv84eavHiaiceqxibJxCfHe/46",
//     "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
//     "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
// }

var pool = require('./DBConnectPool');
var logger = require('../../common/Logger/Logger');
module.exports={

    userLogin: function (openid, phone, callBack) {
        pool.getPoolConnection('mtsc', function (err, connection) {
            //检查是否存在用户，存在






            var sql = 'update member_user set openid=? where phone_number = ? ';
            if (err) {
                console.log('Error: get connection from pool in LogDao: ', err);
                return;
            }
            connection.query(sql, [openid, phone], function (queryErr, rows) {
                if (queryErr) {
                    console.log('select member_id from member_user: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    if (rows && rows.affectedRows == 1) {
                        callBack({
                            isSuccess: true,
                            message: '登录成功'
                        });
                        return;
                    } else {
                        callBack({
                            isSuccess: false,
                            message: '查无手机'
                        });
                    }
                }
                connection.release();
            });
            //  var j=1;
        })
    },
    addMember:function(user,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in member: ', err);
                return;
            }
            var param=[user.openid,user.nickname,user.sex,user.phone_number,user.province,user.city,user.country,user.headimgurl,user.privilege.toString()];
            var sql = 'insert into member_user (openid,nickname,sex,phone_number,province,city,country,headimgurl,privilege) values(?,?,?,?,?,?,?,?,?)';
            connection.query(sql, param, function (queryErr, rows) {
                if (queryErr) {
                    console.log('insert into member_user failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();
            })
        })

    },
    updateMember:function(_param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in member_user: ', err);
                return;
            }
            var sql = 'update member set user_name = ? where member_id = ? ';
            connection.query(sql, [_param], function (queryErr, rows) {
                if (queryErr) {
                    console.log('update member failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();
            })
        })
    },
    selectByopenid:function(_param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in member: ', err);
                return;
            }
            var sql = 'select * from member_user where openid = ? ';
            connection.query(sql, [_param], function (queryErr, rows) {
                if (queryErr) {
                    console.log('select member_user failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();
            })
        })
    },
    selectByPhone:function(_param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in member: ', err);
                return;
            }
            var sql = 'select * from member_user where phone_number = ? ';
            connection.query(sql, [_param], function (queryErr, rows) {
                if (queryErr) {
                    console.log('select member_user failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();
            })
        })
    }
}